'use strict';

const Controller = require("../core/base");
const Model = require("../model/index");
const Sequelize = require("sequelize");
const dbc = require('../DBConfig/config');
const moment = require("moment");

class OrderListController extends Controller {
    // PC后台管理系统端
    async orderList() {
        const { ctx } = this;
        const body = ctx.request.query;
        const where = {};
        if (body.username) {
            where.contact_name = body.username;
        }
        try {
            const { count, rows } = await Model.OrderList.findAndCountAll({
                where: where
            });
            this.success({
                data: rows,
                total: count
            });
        } catch(err) {
            this.fail(err);
        }
    }

    // 小程序端订单列表
    async orderListMini() {
        const { ctx } = this;
        const body = ctx.request.body;
        let where = {
            user_id: body.userid
        };
        if (body.status) {
            // status表示订单状态
            where.status = body.status;
        }
        try {
            const rows = await Model.OrderList.findAll({
                where: where,
                attributes: [[Sequelize.col("tourist_place.name_brief"), "name"], Sequelize.col("tourist_place.cover") ,"*"],
                include: [
                    {
                        model: Model.TouristPlace,
                        attributes: [],
                    }
                ],
                raw: true
            }).then(async res => {
                let arr = res || [];
                for(let i = 0; i < arr.length; i++) {
                    res[i].follows = await Model.OrderListFlow.findAll({ where: {order_list_id: arr[i].id} });
                    res[i].create_time = moment(res[i].create_time).format("YYYY-MM-DD HH:mm:ss");
                    res[i].update_time = moment(res[i].update_time).format("YYYY-MM-DD HH:mm:ss");
                }
                return arr;
            })

            this.success({
                data: rows
            })
        } catch(err) {
            this.fail(err);
        }
    }

    // 创建订单
    async createOrder() {
        const { ctx } = this;
        const body = ctx.request.body;

        try {
            const t = await dbc.transaction();

            let datas = await Model.OrderList.create({
                tourist_place_id: body.place_id,
                status: 1,
                user_id: body.userid,
                create_time: new Date(),
                update_time: new Date(),
                contact_name: body.self.name,
                contact_phone: body.self.phone,
                contact_email: body.self.email,
                contact_remarks: body.self.remarks,
                total_price: body.total_price
            }, { transaction: t });

            let flows = body.other.map(item => {
                return {
                    ...item,
                    order_list_id: datas.id
                }
            });

            await Model.OrderListFlow.bulkCreate(flows, { transaction: t })

            t.commit();

            this.success();
        } catch (err) {
            await t.rollback();
            this.fail(err);
        }
    }

    // 删除订单
    async delOrder() {
        const { ctx } = this;
        const body = ctx.request.body;
        try {
            await Model.OrderList.destroy({
                where: {
                    id: body.id
                }
            })

            await Model.OrderListFlow.destroy({
                where: {
                    order_list_id: body.id
                }
            })
            this.success();
        } catch(err) {
            this.fail(err);
        }
    }
}

module.exports = OrderListController;